package aes.client;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.*;

import javax.swing.*;
import javax.swing.border.LineBorder;

import common.CommandMessage;

public class ExamExecutionPrincipalUI extends JPanel implements ActionListener, Observer{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 6888206463043351148L;
	
	private ClientUI ui;
	private Client c;
	private JButton btnBack; 
	private JButton btnAccept; 
	private JButton btnReject; 
	private JLabel lblCodesList;
	private JComboBox cmbTimeApprovementList;
	private JLabel lblNotes;
	private JTextField textNotes;
	private boolean loading;
	private JLabel lblAdditionalTime;
	private JTextField textAddTime;

	@SuppressWarnings("deprecation")
	public ExamExecutionPrincipalUI( Client c,ClientUI ui) {
		super();
		this.ui = ui;
		this.c = c;
		JFrame frame = new JFrame();
		frame.setSize(640, 480);
		frame.getContentPane().add(this);
		
		c.addObserver(this);
		setLayout(new GridBagLayout());
		
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		panel.setPreferredSize(new Dimension(600,400));
		
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(5, 0, 5, 0);
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 0;
		add(panel,gbc_panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0};
		gbl_panel.columnWeights = new double[]{0.0, 1.0};
		panel.setLayout(gbl_panel);
		
		JLabel lblExamExecTeacherTitle = new JLabel("Exams Time Change Approvement:");
		lblExamExecTeacherTitle.setHorizontalAlignment(SwingConstants.CENTER);
		lblExamExecTeacherTitle.setVerticalAlignment(SwingConstants.TOP);
		lblExamExecTeacherTitle.setBounds(7, 10, 70, 17);
		lblExamExecTeacherTitle.setFont(new Font("Tahoma", Font.BOLD, 14));
		
		
		GridBagConstraints gbc_lblExamExecTeacherTitle = new GridBagConstraints();
		gbc_lblExamExecTeacherTitle.anchor = GridBagConstraints.NORTH;
		gbc_lblExamExecTeacherTitle.gridwidth = 2;
		gbc_lblExamExecTeacherTitle.fill = GridBagConstraints.BOTH;
		gbc_lblExamExecTeacherTitle.insets = new Insets(5, 0, 5, 0);
		gbc_lblExamExecTeacherTitle.gridx = 0;
		gbc_lblExamExecTeacherTitle.gridy = 0;
		panel.add(lblExamExecTeacherTitle, gbc_lblExamExecTeacherTitle);
		
		lblCodesList = new JLabel("Code List:");
		lblCodesList.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_lblCode = new GridBagConstraints();
		gbc_lblCode.anchor = GridBagConstraints.EAST;
		gbc_lblCode.insets = new Insets(0, 0, 5, 5);
		gbc_lblCode.gridx = 0;
		gbc_lblCode.gridy = 2;
		panel.add(lblCodesList, gbc_lblCode);
		
		cmbTimeApprovementList = new JComboBox();
		cmbTimeApprovementList.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				LoadNotesAndAddTime();
			}
		});
		GridBagConstraints gbc_cmbTimeApprovementList = new GridBagConstraints();
		gbc_cmbTimeApprovementList.insets = new Insets(0, 0, 5, 10);
		gbc_cmbTimeApprovementList.fill = GridBagConstraints.HORIZONTAL;
		gbc_cmbTimeApprovementList.gridx = 1;
		gbc_cmbTimeApprovementList.gridy = 2;
		panel.add(cmbTimeApprovementList, gbc_cmbTimeApprovementList);
		
		btnBack = new JButton("Back", new ImageIcon(ExamsManagerUI.class.getResource("/aes/client/images/back.png")));
		btnBack.addActionListener(this);
		
		lblAdditionalTime = new JLabel("Additional Time:");
		GridBagConstraints gbc_lblAdditionalTime = new GridBagConstraints();
		gbc_lblAdditionalTime.anchor = GridBagConstraints.EAST;
		gbc_lblAdditionalTime.insets = new Insets(0, 0, 5, 5);
		gbc_lblAdditionalTime.gridx = 0;
		gbc_lblAdditionalTime.gridy = 3;
		panel.add(lblAdditionalTime, gbc_lblAdditionalTime);
		
		textAddTime = new JTextField();
		GridBagConstraints gbc_textAddTime = new GridBagConstraints();
		gbc_textAddTime.insets = new Insets(0, 0, 5, 10);
		gbc_textAddTime.fill = GridBagConstraints.HORIZONTAL;
		gbc_textAddTime.gridx = 1;
		gbc_textAddTime.gridy = 3;
		panel.add(textAddTime, gbc_textAddTime);
		textAddTime.setColumns(10);
		textAddTime.disable();
		
		lblNotes = new JLabel("Notes:");
		lblNotes.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_lblNotes = new GridBagConstraints();
		gbc_lblNotes.anchor = GridBagConstraints.EAST;
		gbc_lblNotes.insets = new Insets(0, 0, 5, 5);
		gbc_lblNotes.gridx = 0;
		gbc_lblNotes.gridy = 4;
		panel.add(lblNotes, gbc_lblNotes);
		
		textNotes = new JTextField();
		GridBagConstraints gbc_textNotes = new GridBagConstraints();
		gbc_textNotes.insets = new Insets(0, 0, 5, 10);
		gbc_textNotes.fill = GridBagConstraints.HORIZONTAL;
		gbc_textNotes.gridx = 1;
		gbc_textNotes.gridy = 4;
		panel.add(textNotes, gbc_textNotes);
		textNotes.setColumns(10);
		textNotes.disable();
		
		btnReject = new JButton("Reject", new ImageIcon(ExamsManagerUI.class.getResource("/aes/client/images/Cancel.png")));
		btnReject.addActionListener(this);
		GridBagConstraints gbc_btnReject = new GridBagConstraints();
		gbc_btnReject.anchor=GridBagConstraints.EAST;
		gbc_btnReject.insets = new Insets(5, 5, 5, 10);
		gbc_btnReject.gridwidth=2;

		gbc_btnReject.gridx = 0;
		gbc_btnReject.gridy = 6;
		panel.add(btnReject, gbc_btnReject);
		
		btnAccept = new JButton("Accept", new ImageIcon(ExamsManagerUI.class.getResource("/aes/client/images/Plus.png")));
		btnAccept.addActionListener(this);
		GridBagConstraints gbc_btnAccept = new GridBagConstraints();
		gbc_btnAccept.insets = new Insets(5, 5, 5, 10);
		gbc_btnAccept.anchor=GridBagConstraints.WEST;
		gbc_btnAccept.gridwidth=2;
		
		gbc_btnAccept.gridx = 1;
		gbc_btnAccept.gridy = 6;
		panel.add(btnAccept, gbc_btnAccept);
		
		

		GridBagConstraints gbc_btnBack = new GridBagConstraints();
		gbc_btnBack.anchor = GridBagConstraints.WEST;
		gbc_btnBack.insets = new Insets(5, 10, 0, 5);
		gbc_btnBack.gridx = 0;
		gbc_btnBack.gridy = 10;
		panel.add(btnBack, gbc_btnBack);
		btnBack.setBounds(353, 7, 57, 23);
		
		
		
		// Bind data.
		Initialize();
		
		
	}
	/**
	 * Initialize interface
	 */

	private void Initialize() {
		// Get exam code
		loading = true;
		CommandMessage msg = new CommandMessage(CommandMessage.GET_EXAMS_CODE_WHICH_TIME_CHANGED, new Object[] {});
		try {
			c.sendToServer(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * Load notes according to exam code
	 */
	private void LoadNotesAndAddTime() {
		if (loading)
			return;
		// Get exams id's by subject and course.
		CommandMessage msg = new CommandMessage(CommandMessage.GET_NOTES_BY_CODE, new Object[] {(String)(cmbTimeApprovementList.getSelectedItem()).toString()});
		try {
			c.sendToServer(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		CommandMessage ms1 = new CommandMessage(CommandMessage.GET_ADDITIONAL_TIME_BY_CODE, new Object[] {(String)(cmbTimeApprovementList.getSelectedItem()).toString()});
		try {
			c.sendToServer(ms1);
		} catch (IOException e) {
			e.printStackTrace();
		}		

		
	}



	@SuppressWarnings("static-access")
	@Override
	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub
		if (arg1 instanceof CommandMessage) {
			CommandMessage ms=(CommandMessage)arg1;
			if (ms.getCommand().equals(CommandMessage.ACKNOWLEDGE_MESSAGE) && ((String)ms.getParams()[0]).equals(CommandMessage.GET_EXAMS_CODE_WHICH_TIME_CHANGED)) {
				// execute code list updated.
				String[] codes = (String[]) ms.getParams()[1];

				cmbTimeApprovementList.removeAllItems();
				if(codes!=null){
					for (String c:codes) 
						cmbTimeApprovementList.addItem(c);

			
				}
				
				loading = false;
				LoadNotesAndAddTime();
			}
		
	
		    if (ms.getCommand().equals(CommandMessage.ACKNOWLEDGE_MESSAGE) && ((String)ms.getParams()[0]).equals(CommandMessage.GET_NOTES_BY_CODE)) {
			// Notes update.
			   String s =(String)((ms.getParams()[1].toString()));
			   
			   textNotes.removeAll();
			   if(s!=null)
				   textNotes.setText(s);
			   
			   
			}
		    
		    if (ms.getCommand().equals(CommandMessage.ACKNOWLEDGE_MESSAGE) && ((String)ms.getParams()[0]).equals(CommandMessage.GET_ADDITIONAL_TIME_BY_CODE)) {
				// Notes update.
				   Integer t =Integer.parseInt((String)(ms.getParams()[1].toString()));
				   textAddTime.removeAll();
				   if(t!=0)
					   textAddTime.setText(String.valueOf(t) +" minutes.");
				   
				   
				}
		    
		    if (ms.getCommand().equals(CommandMessage.ACKNOWLEDGE_MESSAGE) && ((String)ms.getParams()[0]).equals(CommandMessage.ACCEPT_OR_REJECT_ADDITIONAL_TIME)) {
				int res = new Integer(ms.getParams()[1].toString());
				if (res == -1) {
					
					ui.ShowMessageBox("ERROR", "Can't accept additonal time to exam execution", JOptionPane.ERROR_MESSAGE);
				}
				else if(res > 0){
					// Accepted
					ui.ShowMessageBox("Information", "Additional Time was accepted" , JOptionPane.INFORMATION_MESSAGE);

					textAddTime.setText("");
					textNotes.setText("");
					Initialize();

					
				}
				else if (res == 0) {
					
					ui.ShowMessageBox("Information", "Additional Time was rejected" , JOptionPane.INFORMATION_MESSAGE);
					textAddTime.setText("");
					textNotes.setText("");
					Initialize();

				}
				else{

					ui.ShowMessageBox("ERROR", "Can't reject additonal time to exam execution", JOptionPane.ERROR_MESSAGE);
				}
		    }
		    
		}
	}
		



	@SuppressWarnings("static-access")
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(btnBack)) {
			ui.SetContent(ui.MAIN_SCREEN);
		}
		if (e.getSource().equals(btnAccept)) {
			CommandMessage msg = new CommandMessage(CommandMessage.ACCEPT_OR_REJECT_ADDITIONAL_TIME, new Object[] {(String)(cmbTimeApprovementList.getSelectedItem()).toString(),true});
			try {
				c.sendToServer(msg);
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		if (e.getSource().equals(btnReject)) {
			CommandMessage msg = new CommandMessage(CommandMessage.ACCEPT_OR_REJECT_ADDITIONAL_TIME, new Object[] {(String)(cmbTimeApprovementList.getSelectedItem()).toString(),false});
			try {
				c.sendToServer(msg);
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		// TODO Auto-generated method stub
		
	}
	

}
